package org.dromara.xxmj.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.xxmj.domain.CourseCategory;
import org.dromara.xxmj.domain.dto.CourseQueryDto;
import org.dromara.xxmj.domain.vo.*;
import org.dromara.xxmj.service.ICourseCategoryService;
import org.dromara.xxmj.service.ICourseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器
 *
 * @author Vincent
 */
@Slf4j
@Tag(name = "课程管理API")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xxmj/course")
public class CourseController extends BaseController {

    private final ICourseService courseService;
    private final ICourseCategoryService categoryService;

    /**
     * 获取课程列表
     */
    @Operation(summary = "获取课程列表")
    @GetMapping("/list")
    public R<TableDataInfo<CourseListVO>> list(CourseQueryDto dto, PageQuery pageQuery) {
        return R.ok(courseService.listCourse(dto, pageQuery));
    }

    /**
     * 获取课程详情
     */
    @Operation(summary = "获取课程详情")
    @GetMapping("/detail/{id}")
    public R<CourseDetailVo> getDetail(@Parameter(description = "课程ID") @PathVariable Long id) {
        return R.ok(courseService.getCourseDetail(id));
    }

    /**
     * 获取用户已购买的课程列表
     */
    @Operation(summary = "获取用户已购买的课程列表")
    @GetMapping("/my")
    public R<TableDataInfo<CourseMyOrderVO>> listUserCourse(CourseQueryDto query, PageQuery pageQuery) {
        return R.ok(courseService.listUserCourse(query, pageQuery));
    }

    /**
     * 检查用户是否已购买课程
     */
    @Operation(summary = "检查用户是否已购买课程")
    @GetMapping("/checkPurchase/{courseId}")
    public R<Boolean> checkPurchase(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        if (courseService.checkUserPurchased(courseId)) {
            return R.ok("您已购买过该课程");
        }
        return R.fail("您还未购买该课程");
    }

    /**
     * 获取课程分类列表
     */
    @Operation(summary = "获取课程分类列表")
    @GetMapping("/category/list")
    public R<List<CourseCategory>> listCategories() {
        return R.ok(categoryService.listEnabledCategories());
    }

    /**
     * 获取课程章节列表
     */
    @Operation(summary = "获取课程章节列表")
    @GetMapping("/chapters/{courseId}")
    public R<List<CourseChapterVo>> getChapters(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        return R.ok(courseService.getCourseChapters(courseId));
    }

    /**
     * 获取学生学习进度
     *
     * @Author 王晨沣
     */
    @Operation(summary = "获取学生学习进度")
    @GetMapping("/progress/{courseId}")
    public R<Integer> getProgress(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        return R.ok(courseService.getUserCourseProgress(courseId));
    }




}
